using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Microsoft.Ink;

namespace SpellingQuiz
{
	/// <summary>
	/// Summary description for SpellingEntryPanel.
	/// </summary>
	public class SpellingEntryPanel : System.Windows.Forms.UserControl
	{
        public event EventHandler EntryPanelActivated;
        public event EventHandler EntryPanelDeactivated;

        private int ordinal;
        static bool InkAvailable, showInk;
        static private InkEntryForm inkForm;
        static private Form containingForm;

        public static Form ContainingForm
        {
            get { return SpellingEntryPanel.containingForm; }
            set { SpellingEntryPanel.containingForm = value; }
        }

        // Declare the Ink Collector object
        // private InkCollector myInkCollector;

        // myRecognizers is used to retrieve the number of installed recognizers
        static Recognizers myRecognizers = new Recognizers();

        public int OrdinalPosition
        {
            get { return ordinal; }
            set
            {
                ordinal = value;
                numberLabel.Text = string.Format("{0}",ordinal);
            }
        }

		private System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SpellingEntryPanel));

        private System.Windows.Forms.Button SpeakButton;
        public TextBox QuizTextBox;
		private System.Windows.Forms.TextBox AnswerTextBox;

		private SpellingEntry entryValue;
        private WaveLib.SoundUserControl soundUserControl1;
        private Label numberLabel;

        private static PenInputPanel pipShared = null;

        static SpellingEntryPanel()
        {
            InkAvailable = myRecognizers.Count > 0;

            if (InkAvailable)
            {
                inkForm = new InkEntryForm();
                inkForm.Visible = false;
            }
        }

        public static bool ShowInkEntry
        {
            set
            {
                showInk = value;

                if (inkForm != null)
                {
                    inkForm.Visible = showInk;
                }
            }
        }

		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SpellingEntryPanel( SpellingEntry entry)
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			this.Entry = entry;
			AnswerTextBox.Visible = false;
			AnswerTextBox.Enabled = false;
            soundUserControl1.SoundSample = entry.SoundRecordingBytes;
			QuizTextBox.Enter += new EventHandler(QuizTextBox_Enter);
			SpeakButton.TabStop = false;
			QuizTextBox.KeyUp += new KeyEventHandler(QuizTextBox_KeyUp);

            if (inkForm != null && containingForm != null)
            {
                inkForm.CurrentEntryPanel = this;
                inkForm.Top = containingForm.Top;
                inkForm.Left = containingForm.Left + containingForm.Width;
            }

        }

		public bool IsCorrect
		{
			get
            {
                return (StringComparer.CurrentCultureIgnoreCase.Compare(QuizTextBox.Text.Trim(), entryValue.SpellingWord)== 0);
				//return QuizTextBox.Text.Trim().ToLower().Equals( entryValue.SpellingWord );
			}
		}

		public bool Evaluate()
		{
			bool correct = IsCorrect;
			QuizTextBox.Enabled = false;
			AnswerTextBox.Visible = true;

			if( !correct )
			{
				QuizTextBox.BackColor = System.Drawing.Color.Red;
			}

			return correct;
		}

		public SpellingEntry Entry
		{
			get{ return entryValue; }
			set
			{
				entryValue = value;
				AnswerTextBox.Text = entryValue.SpellingWord;
			}
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}

                // dispose any resources associated with our input panels
                if (pipShared != null)
                {
                    pipShared.Dispose();
                }
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.QuizTextBox = new System.Windows.Forms.TextBox();
            this.AnswerTextBox = new System.Windows.Forms.TextBox();
            this.numberLabel = new System.Windows.Forms.Label();
            this.SpeakButton = new System.Windows.Forms.Button();
            this.soundUserControl1 = new WaveLib.SoundUserControl();
            this.SuspendLayout();
            // 
            // QuizTextBox
            // 
            this.QuizTextBox.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.QuizTextBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.QuizTextBox.Location = new System.Drawing.Point(33, 0);
            this.QuizTextBox.MaxLength = 100;
            this.QuizTextBox.Name = "QuizTextBox";
            this.QuizTextBox.Size = new System.Drawing.Size(100, 23);
            this.QuizTextBox.TabIndex = 0;
            // 
            // AnswerTextBox
            // 
            this.AnswerTextBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AnswerTextBox.Location = new System.Drawing.Point(163, 0);
            this.AnswerTextBox.Name = "AnswerTextBox";
            this.AnswerTextBox.Size = new System.Drawing.Size(100, 23);
            this.AnswerTextBox.TabIndex = 2;
            // 
            // numberLabel
            // 
            this.numberLabel.AutoSize = true;
            this.numberLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.numberLabel.Location = new System.Drawing.Point(4, 4);
            this.numberLabel.Name = "numberLabel";
            this.numberLabel.Size = new System.Drawing.Size(26, 17);
            this.numberLabel.TabIndex = 4;
            this.numberLabel.Text = "##";
            // 
            // SpeakButton
            // 
            this.SpeakButton.Image = global::SpellingQuiz.Properties.Resources.Play;
            this.SpeakButton.Location = new System.Drawing.Point(137, 0);
            this.SpeakButton.Name = "SpeakButton";
            this.SpeakButton.Size = new System.Drawing.Size(24, 23);
            this.SpeakButton.TabIndex = 1;
            this.SpeakButton.Click += new System.EventHandler(this.SpeakButton_Click);
            // 
            // soundUserControl1
            // 
            this.soundUserControl1.Location = new System.Drawing.Point(163, -3);
            this.soundUserControl1.Name = "soundUserControl1";
            this.soundUserControl1.ShowProgress = true;
            this.soundUserControl1.ShowRecordButton = true;
            this.soundUserControl1.ShowStopButton = true;
            this.soundUserControl1.Size = new System.Drawing.Size(169, 30);
            this.soundUserControl1.SoundSample = new byte[0];
            this.soundUserControl1.TabIndex = 3;
            this.soundUserControl1.Visible = false;
            // 
            // SpellingEntryPanel
            // 
            this.Controls.Add(this.numberLabel);
            this.Controls.Add(this.soundUserControl1);
            this.Controls.Add(this.AnswerTextBox);
            this.Controls.Add(this.QuizTextBox);
            this.Controls.Add(this.SpeakButton);
            this.Name = "SpellingEntryPanel";
            this.Size = new System.Drawing.Size(313, 24);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void Speak( )
		{
            string spellingPrompt;
            string word = Entry.Pronunciation;
            if (word == null) word = Entry.SpellingWord;

            if (string.IsNullOrEmpty(Entry.ContextPhrase))
            {
                if (Entry.SoundRecording != null)
                {
                    soundUserControl1.Play();
                    return;
                }
                else spellingPrompt = word;
            }
            else
            {
                spellingPrompt = String.Format("{0}. . {1}. . {0}", word, Entry.ContextPhrase);
            }

            Utilities.Speak(spellingPrompt);
		}

		private void SpeakButton_Click(object sender, System.EventArgs e)
		{
			//Speak( );
            QuizTextBox.Focus();
            //Activate();
		}

		private void QuizTextBox_Enter(object sender, EventArgs e)
		{
            Activate();
            Speak();
		}

        public void Activate()
        {
            if (showInk)
            {
                inkForm.CurrentEntryPanel = this;
                inkForm.Visible = true;
            }

            QuizTextBox.BorderStyle = BorderStyle.FixedSingle;

            if (EntryPanelActivated != null)
                EntryPanelActivated(this, null);
        }

        public void Deactivate()
        {
            QuizTextBox.BorderStyle = BorderStyle.Fixed3D;

            if (EntryPanelDeactivated != null)
                EntryPanelDeactivated(this, null);
        }

		private void QuizTextBox_KeyUp(object sender, KeyEventArgs e)
		{
			if( e.KeyCode == Keys.Enter )
			{
				e.Handled = true;
				this.SelectNextControl(this, true, true, true, true);
			}
			else
			{
				e.Handled = false;
				//base.KeyUp(e);
			}
		}

	}
}
